/* 3DGo Game
 * Copyright (C) 2008 Roman Hwang, Eugene Melnichek

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation - version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef _SINGLETON_H_
#define _SINGLETON_H_

#include <cassert>

template<typename T>
class Singleton
{
public:

	Singleton()
	{
		assert(!ms_singleton);
		//use a cunning trick to get the singleton pointing to the start of
		//the whole, rather than the start of the Singleton part of the object
		int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
		ms_singleton = (T*)((int)this + offset);
	}
	~Singleton()
	{
		assert(ms_singleton);
		ms_singleton=0;
	}
	static T& GetInstance()
	{
		assert(ms_singleton);
		return *ms_singleton;
	}
	static T* GetInstancePtr()
	{
		assert(ms_singleton);
		return ms_singleton;
	}
private:

	static T* ms_singleton;

};

template <typename T>
T* Singleton<T>::ms_singleton = 0;

#endif // _SINGLETON_H_